// 验证身份证号格式
function validateID(id) {
    const idPattern = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/;
    return idPattern.test(id);
}

$(document).ready(function () {
    function updateGreeting() {
        const now = new Date();
        const hour = now.getHours();
        let greeting = "你好";

        if (hour < 12) {
            greeting = "早上好";
        } else if (hour < 18) {
            greeting = "下午好";
        } else {
            greeting = "晚上好";
        }

        $("#greeting").text(`${greeting}，陈阳达`);
    }
    updateGreeting();

    function selectFn(params, d1, d2, d3) {
        $(params).on("change", function () {
            const selectedProvince = $(this).find(":selected").text();
            $(d1).hide();
            $(d2).hide();
            if ($(d1).length === 0) {
                $("<span>")
                    .attr("id", d3)
                    .text(selectedProvince)
                    .insertAfter(d4);
            } else {
                $(d1).text(selectedProvince);
            }
        })
    }

    // 上传医师职业证书

    $("#doctorCert").on("change", function (event) {
        const file = event.target.files[0];
        console.log("111",file)
        if (file) {
            const reader = new FileReader();
            reader.onload = function (e) {
                $("#upload-preview").attr("src", e.target.result).show();
                $(".upload-trigger").hide();
            };
            reader.readAsDataURL(file);
        }
    });

    // 选择省份
    const provinces = ["北京", "上海", "广东", "天津", "吉林", "黑龙江"];
    const $provinceSelect = $("#province");
    // selectFn("#province","#select-provice",".arrow","selected-province","#select-provice")
    provinces.forEach((province) => {
        $provinceSelect.append(`<option value="${province}">${province}</option>`);

    })

    $("#province").on("change", function () {
        const selectedProvince = $(this).find(":selected").text();
        $("#select-provice,.arrow").hide();
        if ($("#selected-province").length === 0) {
            $("<span>")
                .attr("id", "selected-province")
                .text(selectedProvince)
                .insertAfter("#select-provice");
        } else {
            $("#selected-province").text(selectedProvince);
        }
    })


    // 选择科室
    const departments = ["儿科", "内科", "外科", "妇产科", "骨科", "心内科", "神经内科"];
    const $departmentSelect = $("#departments");
    departments.forEach((province) => {
        $departmentSelect.append(`<option value="${province}">${province}</option>`);

    })

    $("#departments").on("change", function () {
        const selectedDepartments = $(this).find(":selected").text();
        $("#select-departments,.arrow-department").hide();
        if ($("#selected-departments").length === 0) {
            $("<span>")
                .attr("id", "selected-departments")
                .text(selectedDepartments)
                .insertAfter("#select-departments");
        } else {
            $("#selected-departments").text(selectedDepartments);
        }
    })
    // 选择职称
    const professionals = ["主治医生", "副主任医师", "主任医师", "住院医师", "实习医师"];
    const $professionalSelect = $("#custom");
    professionals.forEach((province) => {
        $professionalSelect.append(`<option value="${province}">${province}</option>`);

    })

    $("#custom").on("change", function () {
        const selectedProfessionals = $(this).find(":selected").text();
        $("#select-customs,.arrow-custom").hide();
        if ($("#selected-custom").length === 0) {
            $("<span>")
                .attr("id", "selected-custom")
                .text(selectedProfessionals)
                .insertAfter("#select-customs");
        } else {
            $("#selected-custom").text(selectedProfessionals);
        }
    })

    $(".circle-checkbox").on("click", function() {
        $(this).toggleClass("checked"); 
    });


    $('#authForm').on('submit', function (e) {
        e.preventDefault();

        // 获取表单值
        const realName = $('#realName').val();
        const idNumber = $('#idNumber').val();
        const doctorCert = $('#doctorCert').val();
        const certNumber = $('#certNumber').val();
        const province = $('#province').val();
        const medicalInstitution = $('#medicalInstitution').val();
        const departments = $('#departments').val();
        const custom = $('#custom').val();
        const agreement = $('#agreement').is(':checked');

        // 检查身份证号
        if (!validateID(idNumber)) {
            alert('请输入有效的身份证号');
            return;
        }


        // 检查是否同意协议
        if (!agreement) {
            alert('请阅读并同意实名认证协议');
            return;
        }

        // 保存数据到 localStorage
        const formData = {
            realName,
            idNumber,
            doctorCert,
            certNumber,
            province,
            medicalInstitution,
            departments,
            custom
        };
        localStorage.setItem('authFormData', JSON.stringify(formData));
        alert('提交成功，信息已保存');
    });
});